package com.xhs.utils;

import okhttp3.*;

import java.io.IOException;

/**
 * Created by jm on 2017/12/1.
 */
public class HttpClientHelper {

    private OkHttpClient client;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private HttpClientHelper(){

        this.client = new OkHttpClient.Builder().build();
        System.out.println(" == ==== == == = = =  =  OkHttpClient init.. == ==== == == = = =  =");
    }

    private static class SingletonHolder{
        public static HttpClientHelper client = new HttpClientHelper();
    }

    public static HttpClientHelper getInstance(){
        return SingletonHolder.client;
    }

    public OkHttpClient getClient(){
        return this.client;
    }

    public void reqGet(String reqUrl){
        Request request = new Request.Builder().url(reqUrl).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("[失败] 请求发送 --->  分发nginx ");
                e.printStackTrace();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("[成功] 请求发送 --->  分发nginx ");
            }
        });
    }


    public String reqGetResponse(String reqUrl){
        Request request = new Request.Builder().url(reqUrl).build();
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        }catch (Exception e){
            e.printStackTrace();

        }
        return null;
    }



}
